/**
 * @file lv_port_indev_templ.c
 *
 */

 /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev.h"
#include <windows.h>

#define AWESOME_ICON_POINTER	"\xef\x89\x85" // F245

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

//static void touchpad_init(void);
//static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static bool touchpad_is_pressed(void);
//static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);

static void mouse_init(void);
static bool mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool mouse_is_pressed(void);
static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y);

static void keypad_init(void);
static bool keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static uint32_t keypad_get_key(void);

//static void encoder_init(void);
static bool encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static void encoder_handler(void);

//static void button_init(void);
//static bool button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static int8_t button_get_pressed_id(void);
//static bool button_is_pressed(uint8_t id);

/**********************
 *  STATIC VARIABLES
 **********************/
//lv_indev_t * indev_touchpad;
lv_indev_t * indev_mouse;
lv_indev_t * indev_keypad;
lv_indev_t * indev_encoder;
//lv_indev_t * indev_button;

//static int32_t encoder_diff;
//static lv_indev_state_t encoder_state;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
	/* Here you will find example implementation of input devices supported by LittelvGL:
	 *  - Touchpad
	 *  - Mouse (with cursor support)
	 *  - Keypad (supports GUI usage only with key)
	 *  - Encoder (supports GUI usage only with: left, right, push)
	 *  - Button (external buttons to press points on the screen)
	 *
	 *  The `..._read()` function are only examples.
	 *  You should shape them according to your hardware
	 */


	static lv_indev_drv_t indev_drv_kbd;
	static lv_indev_drv_t indev_drv_mouse;
	static lv_indev_drv_t indev_drv_encoder;

	/*------------------
	 * Mouse
	 * -----------------*/

	/*Initialize your touchpad if you have*/
	mouse_init();

	/*Register a mouse input device*/
	lv_indev_drv_init(&indev_drv_mouse);
	indev_drv_mouse.type = LV_INDEV_TYPE_POINTER;
	indev_drv_mouse.read_cb = mouse_read;
	indev_mouse = lv_indev_drv_register(&indev_drv_mouse);

	/*Set cursor. For simplicity set a HOME symbol now.*/
	lv_obj_t * mouse_cursor = lv_img_create(lv_disp_get_scr_act(0));
	lv_img_set_src(mouse_cursor, AWESOME_ICON_POINTER);
	lv_indev_set_cursor(indev_mouse, mouse_cursor);

	// mouse wheel
	lv_indev_drv_init(&indev_drv_encoder);
	indev_drv_encoder.type = LV_INDEV_TYPE_ENCODER;
	indev_drv_encoder.read_cb = encoder_read;
	indev_encoder = lv_indev_drv_register(&indev_drv_encoder);

	/*------------------
	 * Keypad
	 * -----------------*/

	/*Initialize your keypad or keyboard if you have*/
	keypad_init();

	/*Register a keypad input device*/
	lv_indev_drv_init(&indev_drv_kbd);
	indev_drv_kbd.type = LV_INDEV_TYPE_KEYPAD;
	indev_drv_kbd.read_cb = keypad_read;
	indev_keypad = lv_indev_drv_register(&indev_drv_kbd);

	/* Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
	 * add objects to the group with `lv_group_add_obj(group, obj)`
	 * and assign this input device to group to navigate in it:
	 * `lv_indev_set_group(indev_keypad, group);` */
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Mouse
 * -----------------*/

// in ch9350_input.c
extern int mouse_x;
extern int mouse_y;
extern int mouse_key;
extern signed char   mouse_wheel;

/* Initialize your mouse */
static void mouse_init(void)
{
}

/* Will be called by the library to read the mouse */
static bool mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
	/*Get the current x and y coordinates*/
	mouse_get_xy(&data->point.x, &data->point.y);

	/*Get whether the mouse button is pressed or released*/
	if(mouse_is_pressed()) {
		data->state = LV_INDEV_STATE_PR;
	} else {
		data->state = LV_INDEV_STATE_REL;
	}

	/*Return `false` because we are not buffering and no more data to read*/
	return false;
}

/*Return true is the mouse button is pressed*/
static bool mouse_is_pressed(void)
{
	if (mouse_key & 1) {
		return true;
	}

	return false;
}

/*Get the x and y coordinates if the mouse is pressed*/
static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y)
{
	(*x) = mouse_x;
	(*y) = mouse_y;
}

/* Will be called by the library to read the encoder */
static bool encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
	data->enc_diff = -mouse_wheel;
	data->state = (mouse_key & 4) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;

	mouse_wheel = 0;

	/*Return `false` because we are not buffering and no more data to read*/
	return false;
}

/*------------------
 * Keypad
 * -----------------*/

#define KEY_SCODE(c) (key_bitmap[(c) / 8] & 1 << ((c) % 8))
extern unsigned char key_bitmap[32];

/* Initialize your keypad */
static void keypad_init(void)
{
	/*Your code comes here*/
}

//extern int event_gui_done;

/* Will be called by the library to read the mouse */
static bool keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
	static uint32_t last_key = 0;

	/*Get the current x and y coordinates*/
	mouse_get_xy(&data->point.x, &data->point.y);

	/*Get whether the a key is pressed and save the pressed key*/
	uint32_t act_key = keypad_get_key();
	if(act_key != 0) {
		data->state = LV_INDEV_STATE_PR;
		last_key = act_key;
		//sysprintf("LK: (%d)\r\n", act_key);
	} else {
//		if (last_key == LV_KEY_ESC) event_gui_done = 1;
		data->state = LV_INDEV_STATE_REL;
	}

	data->key = last_key;
	if (!act_key) last_key = 0;

	/*Return `false` because we are not buffering and no more data to read*/
	return false;
}

/*Get the currently being pressed key.  0 if no key is pressed*/
static uint32_t keypad_get_key(void)
{
	if (GetAsyncKeyState(VK_RIGHT) & 1)
		return LV_KEY_RIGHT;

	if (GetAsyncKeyState(VK_LEFT) & 1)
		return LV_KEY_LEFT;

	if (GetAsyncKeyState(VK_UP) & 1) // UP
		return LV_KEY_PREV;

	if (GetAsyncKeyState(VK_DOWN) & 1)
		return LV_KEY_NEXT; // DOWN

	if (GetAsyncKeyState(VK_RETURN) & 1)
		return LV_KEY_ENTER;

	if (GetAsyncKeyState(VK_ESCAPE) & 1)
		return LV_KEY_ESC;

	if (GetAsyncKeyState(VK_HOME) & 1)
		return LV_KEY_HOME;

	if (GetAsyncKeyState(VK_END) & 1)
		return LV_KEY_END;

	if (GetAsyncKeyState(VK_BACK) & 1)
		return LV_KEY_BACKSPACE;

	return 0;
}
